/**  
 * All rights Reserved, Designed By daiw
 * @Title:  SysUserController.java   
 * @Package org.easy.system.manage.controller   
 * @Description: 
 * @author: daiw     
 * @date:   2019年10月17日 下午3:34:41   
 * @Copyright: 2019 daiw Inc. All rights reserved. 
 */
package org.easy.system.manage.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.easy.system.manage.dto.SaveUserParam;
import org.easy.system.manage.dto.UserQueryParam;
import org.easy.system.manage.entity.SysUser;
import org.easy.system.manage.service.SysUserService;
import org.easyframework.core.entity.ProcessResult;
import org.easyframework.core.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.system.manage.facade.entity.UserInfoSession;
import org.system.manage.facade.entity.ValidLoginResult;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: SysUserController
 * @Description: 用户管理控制器类
 * @author: daiw
 * @date: 2019年10月17日 下午3:34:41
 */
@ApiModel("用户管理")
@RestController
@RequestMapping("user")
public class SysUserController extends BaseController {

	@Autowired
	private SysUserService sysUserService;

	@ApiOperation(value = "被sso-server调用", hidden = true)
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public ValidLoginResult login(@RequestBody Map<String, String> param) {
		if (!param.containsKey("loginInfo")) {
			return null;
		}
		try {
			return sysUserService.validLogin(param.get("loginInfo"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@ApiOperation(value = "保存用户信息")
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<?> save(@RequestBody SaveUserParam param, HttpServletRequest request) {
		ProcessResult<?> processResult = sysUserService.insert(param, getUserInfo(request));
		return ResponseResult.getInst(processResult);
	}

	@ApiOperation(value = "获取用户信息")
	@RequestMapping(value = "queryUserInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<UserInfoSession> queryUserInfo(HttpServletRequest request) {
		UserInfoSession userInfoSession = getUserInfoSession(request);
		return ResponseResult.getInst(ResponseResult.SUCCESS, userInfoSession);
	}

	@ApiOperation(value = "分页查询用户信息")
	@RequestMapping(value = "queryByPage", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<List<SysUser>> queryByPage(@RequestBody UserQueryParam queryParam) {
		ProcessResult<List<SysUser>> processResult = sysUserService.queryByPage(queryParam);
		return ResponseResult.getInst(ResponseResult.SUCCESS, processResult);
	}

	@ApiOperation(value = "查询用户详情")
	@ApiImplicitParam(paramType = "query", name = "userId", value = "用户编号", required = true, dataType = "String")
	@RequestMapping(value = "queryDetail", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<SysUser> queryDetail(@RequestParam(value = "userId", required = true) String userId) {
		ProcessResult<SysUser> processResult = sysUserService.queryDetail(Long.parseLong(userId));
		return ResponseResult.getInst(ResponseResult.SUCCESS, processResult);
	}

	@ApiOperation(value = "修改用户状态")
	@ApiImplicitParams({ //
			@ApiImplicitParam(paramType = "form", name = "userId", value = "用户编号", required = true, dataType = "String"), //
			@ApiImplicitParam(paramType = "form", name = "status", value = "状态", required = true, dataType = "Integer")//
	})
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	@ResponseBody
	public ResponseResult<?> updateStatus(//
			@RequestParam(value = "userId", required = true) String userId, //
			@RequestParam(value = "status", required = true) Integer status) {
		ProcessResult<?> processResult = sysUserService.updateStatus(Long.parseLong(userId), status);
		return ResponseResult.getInst(ResponseResult.SUCCESS, processResult);
	}

}
